import { defineStore } from 'pinia'

export const useSettingsStore = defineStore('settings', {
  state: () => ({
    theme: 'light', // 'light', 'dark', 'system'
    defaultView: 'today', // 'today', 'all', 'calendar'
    defaultCategory: null, // 默认分类ID
    reminderEnabled: true, // 是否启用提醒
    loading: false,
    error: null
  }),
  actions: {
    // 更新设置
    updateSettings(settings) {
      if (settings.theme !== undefined) this.theme = settings.theme
      if (settings.defaultView !== undefined) this.defaultView = settings.defaultView
      if (settings.defaultCategory !== undefined) this.defaultCategory = settings.defaultCategory
      if (settings.reminderEnabled !== undefined) this.reminderEnabled = settings.reminderEnabled
      
      this.saveSettings()
    },
    
    // 加载设置
    async loadSettings() {
      this.loading = true
      this.error = null
      
      try {
        // 这里将来会使用Tauri的文件系统API读取设置
        // 暂时使用localStorage模拟
        const settingsData = localStorage.getItem('todo_settings')
        if (settingsData) {
          const settings = JSON.parse(settingsData)
          this.theme = settings.theme || 'light'
          this.defaultView = settings.defaultView || 'today'
          this.defaultCategory = settings.defaultCategory || null
          this.reminderEnabled = settings.reminderEnabled !== undefined ? settings.reminderEnabled : true
        }
      } catch (err) {
        console.error('加载设置失败:', err)
        this.error = '加载设置失败'
      } finally {
        this.loading = false
      }
    },
    
    // 保存设置
    saveSettings() {
      try {
        // 这里将来会使用Tauri的文件系统API保存设置
        // 暂时使用localStorage模拟
        const settings = {
          theme: this.theme,
          defaultView: this.defaultView,
          defaultCategory: this.defaultCategory,
          reminderEnabled: this.reminderEnabled
        }
        
        localStorage.setItem('todo_settings', JSON.stringify(settings))
      } catch (err) {
        console.error('保存设置失败:', err)
        this.error = '保存设置失败'
      }
    }
  }
}) 